/**
 * Theme presets — six curated palettes on html[data-theme].
 *
 * Each theme owns its whole token set: surfaces, text, the action colour (replaces the admin accent),
 * the header bar and table heads. Selectors carry :not(#_) so they win over the older layers
 * (app.css body.theme-dark, fac-mobile-first, fac-theme-contrast) whatever the load order.
 *
 * Light family: light (Navy Ledger), blue (Ocean), calm (Mist).
 * Dark family (body.theme-dark): dark-blue (Midnight navy), dark-gray (Graphite), solarized.
 * Dark themes use neutral, untinted body text and a desaturated blue action colour for long sessions.
 */

/* ── Light · Navy Ledger (default) ─────────────────────────── */
html[data-theme="light"]:not(#_),
html[data-theme="light"]:not(#_) body {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --border: #dfe4eb;
  --text: #18202e;
  --text-muted: #5b6576;
  --primary: #2e75b6;
  --primary-dark: #1b3a5c;

  --color-bg: #f3f5f8;
  --color-surface: #ffffff;
  --color-border: #dfe4eb;
  --color-text: #18202e;
  --color-text-muted: #5b6576;
  --color-background-alt: #f7f9fb;
  --color-panel-alt: #f7f9fb;
  --color-input-bg: #ffffff;
  --color-hover: #eef3f9;

  --color-accent: #2e75b6;
  --color-primary: #2e75b6;
  --color-primary-ink: #1f5f99;
  --color-primary-ink-dark: #8fbbe6;
  --color-primary-dark: #245d93;
  --color-primary-light: #5b9bd5;
  --color-primary-soft: #e8f1fa;
  --color-primary-rgb: 46, 117, 182;
  --color-primary-light-rgb: 91, 155, 213;
  --color-primary-dark-rgb: 36, 93, 147;

  --topbar-bg: #1b3a5c;
  --topbar-bg-dark: #142c47;
  --topbar-bg-light: #24507d;
  --topbar-bg-darker: #0f2238;
  --topbar-bg-darker-rgb: 15, 34, 56;
  --sidebar-bg-dark: #13293f;
  --sidebar-bg-darker: #0f2238;
  --fac-shell-accent: #2e75b6;
  --fac-topbar-1: #142c47;
  --fac-topbar-2: #1b3a5c;
  --fac-topbar-3: #24507d;
  --fac-bar-1: #142c47;
  --fac-bar-2: #1b3a5c;
  --fac-bar-3: #24507d;
  --fac-sidebar-1: #13293f;
  --fac-sidebar-2: #1b3a5c;

  --fac-table-head-bg: #1f3d5e;
  --fac-table-head-text: #ffffff;
  --color-table-header-bg: #1f3d5e;
  --color-table-row-even: #f8fafc;
  --color-table-row-hover: #eef4fa;
  --color-table-sticky-bg: #ffffff;
  --color-scrollbar-track: #eef1f5;
  --color-scrollbar-thumb: #c3cbd6;
}

/* ── Ocean blue · light, blue-tinted paper ─────────────────── */
html[data-theme="blue"]:not(#_),
html[data-theme="blue"]:not(#_) body {
  --bg: #edf3fa;
  --surface: #ffffff;
  --border: #d3e0ef;
  --text: #13243a;
  --text-muted: #4f6580;
  --primary: #1f6fc5;
  --primary-dark: #11467d;

  --color-bg: #edf3fa;
  --color-surface: #ffffff;
  --color-border: #d3e0ef;
  --color-text: #13243a;
  --color-text-muted: #4f6580;
  --color-background-alt: #f5f9fd;
  --color-panel-alt: #f5f9fd;
  --color-input-bg: #ffffff;
  --color-hover: #e7f0fa;

  --color-accent: #1f6fc5;
  --color-primary: #1f6fc5;
  --color-primary-ink: #195ea8;
  --color-primary-ink-dark: #9cc6f0;
  --color-primary-dark: #185aa2;
  --color-primary-light: #5c9de0;
  --color-primary-soft: #e3eefb;
  --color-primary-rgb: 31, 111, 197;
  --color-primary-light-rgb: 92, 157, 224;
  --color-primary-dark-rgb: 24, 90, 162;

  --topbar-bg: #11467d;
  --topbar-bg-dark: #0c2f55;
  --topbar-bg-light: #1a5a9c;
  --topbar-bg-darker: #0a2645;
  --topbar-bg-darker-rgb: 10, 38, 69;
  --sidebar-bg-dark: #0c2f55;
  --sidebar-bg-darker: #0a2645;
  --fac-shell-accent: #1f6fc5;
  --fac-topbar-1: #0c2f55;
  --fac-topbar-2: #11467d;
  --fac-topbar-3: #1a5a9c;
  --fac-bar-1: #0c2f55;
  --fac-bar-2: #11467d;
  --fac-bar-3: #1a5a9c;
  --fac-sidebar-1: #0a2645;
  --fac-sidebar-2: #11467d;

  --fac-table-head-bg: #153f6d;
  --fac-table-head-text: #ffffff;
  --color-table-header-bg: #153f6d;
  --color-table-row-even: #f6f9fd;
  --color-table-row-hover: #e5eef9;
  --color-table-sticky-bg: #ffffff;
  --color-scrollbar-track: #e8eff8;
  --color-scrollbar-thumb: #b9cbe2;
}

/* ── Calm mist · low-contrast slate chrome, muted teal-blue action ─ */
html[data-theme="calm"]:not(#_),
html[data-theme="calm"]:not(#_) body {
  --bg: #eef1f3;
  --surface: #fbfcfc;
  --border: #d8dfe4;
  --text: #26343f;
  --text-muted: #5d6c78;
  --primary: #3a7a96;
  --primary-dark: #34495a;

  --color-bg: #eef1f3;
  --color-surface: #fbfcfc;
  --color-border: #d8dfe4;
  --color-text: #26343f;
  --color-text-muted: #5d6c78;
  --color-background-alt: #f4f6f7;
  --color-panel-alt: #f4f6f7;
  --color-input-bg: #ffffff;
  --color-hover: #e9eef1;

  --color-accent: #3a7a96;
  --color-primary: #3a7a96;
  --color-primary-ink: #2f6479;
  --color-primary-ink-dark: #9fcbdb;
  --color-primary-dark: #2f6479;
  --color-primary-light: #6fa6bd;
  --color-primary-soft: #e5f0f4;
  --color-primary-rgb: 58, 122, 150;
  --color-primary-light-rgb: 111, 166, 189;
  --color-primary-dark-rgb: 47, 100, 121;

  --topbar-bg: #34495a;
  --topbar-bg-dark: #2c3e4c;
  --topbar-bg-light: #3e5869;
  --topbar-bg-darker: #24333f;
  --topbar-bg-darker-rgb: 36, 51, 63;
  --sidebar-bg-dark: #2c3e4c;
  --sidebar-bg-darker: #24333f;
  --fac-shell-accent: #3a7a96;
  --fac-topbar-1: #2c3e4c;
  --fac-topbar-2: #34495a;
  --fac-topbar-3: #3e5869;
  --fac-bar-1: #2c3e4c;
  --fac-bar-2: #34495a;
  --fac-bar-3: #3e5869;
  --fac-sidebar-1: #24333f;
  --fac-sidebar-2: #34495a;

  --fac-table-head-bg: #3a4f60;
  --fac-table-head-text: #ffffff;
  --color-table-header-bg: #3a4f60;
  --color-table-row-even: #f6f8f9;
  --color-table-row-hover: #e8eef1;
  --color-table-sticky-bg: #fbfcfc;
  --color-scrollbar-track: #e8ecef;
  --color-scrollbar-thumb: #c2ccd3;
}

/* ── Midnight navy · dark ──────────────────────────────────── */
html[data-theme="dark-blue"]:not(#_),
html[data-theme="dark-blue"]:not(#_) body {
  --bg: #0d1522;
  --surface: #142031;
  --border: #243248;
  --text: #e2e7ee;
  --text-muted: #9aa7b9;
  --primary: #3678bb;
  --primary-dark: #0e1a2b;

  --color-bg: #0d1522;
  --color-surface: #142031;
  --color-border: #243248;
  --color-text: #e2e7ee;
  --color-text-muted: #9aa7b9;
  --color-background-alt: #111b2a;
  --color-panel-alt: #111b2a;
  --color-input-bg: #101a28;
  --color-hover: #1a2a40;

  --color-accent: #3678bb;
  --color-primary: #3678bb;
  --color-primary-ink: #8cbcec;
  --color-primary-ink-dark: #8cbcec;
  --color-primary-dark: #2c649e;
  --color-primary-light: #7fb2e5;
  --color-primary-soft: #1b2d45;
  --color-primary-rgb: 54, 120, 187;
  --color-primary-light-rgb: 127, 178, 229;
  --color-primary-dark-rgb: 44, 100, 158;

  --topbar-bg: #0e1a2b;
  --topbar-bg-dark: #0a111c;
  --topbar-bg-light: #13233a;
  --topbar-bg-darker: #080e17;
  --topbar-bg-darker-rgb: 8, 14, 23;
  --sidebar-bg-dark: #0a111c;
  --sidebar-bg-darker: #080e17;
  --fac-shell-accent: #3678bb;
  --fac-topbar-1: #0a111c;
  --fac-topbar-2: #0e1a2b;
  --fac-topbar-3: #13233a;
  --fac-bar-1: #0a111c;
  --fac-bar-2: #0e1a2b;
  --fac-bar-3: #13233a;
  --fac-sidebar-1: #080e17;
  --fac-sidebar-2: #0e1a2b;

  --fac-table-head-bg: #18273c;
  --fac-table-head-text: #dbe3ee;
  --color-table-header-bg: #18273c;
  --color-table-row-even: #121d2c;
  --color-table-row-hover: #1a2940;
  --color-table-sticky-bg: #142031;
  --color-scrollbar-track: #0d1522;
  --color-scrollbar-thumb: #2c3b52;
}

/* ── Graphite · neutral dark ───────────────────────────────── */
html[data-theme="dark-gray"]:not(#_),
html[data-theme="dark-gray"]:not(#_) body {
  --bg: #121417;
  --surface: #1a1d21;
  --border: #2c3036;
  --text: #e5e7ea;
  --text-muted: #9da3ab;
  --primary: #4479b0;
  --primary-dark: #16191c;

  --color-bg: #121417;
  --color-surface: #1a1d21;
  --color-border: #2c3036;
  --color-text: #e5e7ea;
  --color-text-muted: #9da3ab;
  --color-background-alt: #16181b;
  --color-panel-alt: #16181b;
  --color-input-bg: #15171a;
  --color-hover: #23272c;

  --color-accent: #4479b0;
  --color-primary: #4479b0;
  --color-primary-ink: #93badf;
  --color-primary-ink-dark: #93badf;
  --color-primary-dark: #37669a;
  --color-primary-light: #87b1dc;
  --color-primary-soft: #1f2a36;
  --color-primary-rgb: 68, 121, 176;
  --color-primary-light-rgb: 135, 177, 220;
  --color-primary-dark-rgb: 55, 102, 154;

  --topbar-bg: #16191c;
  --topbar-bg-dark: #0e1012;
  --topbar-bg-light: #1f2327;
  --topbar-bg-darker: #0b0c0e;
  --topbar-bg-darker-rgb: 11, 12, 14;
  --sidebar-bg-dark: #0e1012;
  --sidebar-bg-darker: #0b0c0e;
  --fac-shell-accent: #4479b0;
  --fac-topbar-1: #0e1012;
  --fac-topbar-2: #16191c;
  --fac-topbar-3: #1f2327;
  --fac-bar-1: #0e1012;
  --fac-bar-2: #16191c;
  --fac-bar-3: #1f2327;
  --fac-sidebar-1: #0b0c0e;
  --fac-sidebar-2: #16191c;

  --fac-table-head-bg: #23272c;
  --fac-table-head-text: #e5e7ea;
  --color-table-header-bg: #23272c;
  --color-table-row-even: #17191c;
  --color-table-row-hover: #24282e;
  --color-table-sticky-bg: #1a1d21;
  --color-scrollbar-track: #121417;
  --color-scrollbar-thumb: #363b42;
}

/* ── Solarized · dark teal, warm text ──────────────────────── */
html[data-theme="solarized"]:not(#_),
html[data-theme="solarized"]:not(#_) body {
  --bg: #002b36;
  --surface: #073642;
  --border: #1a4a56;
  --text: #e4e2d6;
  --text-muted: #93a1a1;
  --primary: #1f75b0;
  --primary-dark: #01303c;

  --color-bg: #002b36;
  --color-surface: #073642;
  --color-border: #1a4a56;
  --color-text: #e4e2d6;
  --color-text-muted: #93a1a1;
  --color-background-alt: #04313c;
  --color-panel-alt: #04313c;
  --color-input-bg: #05313c;
  --color-hover: #0b4250;

  --color-accent: #1f75b0;
  --color-primary: #1f75b0;
  --color-primary-ink: #6cb4e4;
  --color-primary-ink-dark: #6cb4e4;
  --color-primary-dark: #19629a;
  --color-primary-light: #63ade0;
  --color-primary-soft: #0b3d4c;
  --color-primary-rgb: 31, 117, 176;
  --color-primary-light-rgb: 99, 173, 224;
  --color-primary-dark-rgb: 25, 98, 154;

  --topbar-bg: #01303c;
  --topbar-bg-dark: #00212a;
  --topbar-bg-light: #073e4c;
  --topbar-bg-darker: #001a21;
  --topbar-bg-darker-rgb: 0, 26, 33;
  --sidebar-bg-dark: #00212a;
  --sidebar-bg-darker: #001a21;
  --fac-shell-accent: #1f75b0;
  --fac-topbar-1: #00212a;
  --fac-topbar-2: #01303c;
  --fac-topbar-3: #073e4c;
  --fac-bar-1: #00212a;
  --fac-bar-2: #01303c;
  --fac-bar-3: #073e4c;
  --fac-sidebar-1: #001a21;
  --fac-sidebar-2: #01303c;

  --fac-table-head-bg: #0b4554;
  --fac-table-head-text: #eee8d5;
  --color-table-header-bg: #0b4554;
  --color-table-row-even: #053340;
  --color-table-row-hover: #0b4351;
  --color-table-sticky-bg: #073642;
  --color-scrollbar-track: #002b36;
  --color-scrollbar-thumb: #1f5563;
}

/* ── Shell surfaces: flat page, quiet bars ─────────────────── */
html[data-theme]:not(#_) .app-shell {
  background: var(--color-bg);
}

html[data-theme]:not(#_) .sidebar {
  background: linear-gradient(180deg, var(--fac-sidebar-1) 0%, var(--fac-sidebar-2) 100%);
  color: #e2e8f0;
}

html[data-theme]:not(#_) .topbar {
  background: linear-gradient(90deg, var(--fac-bar-1), var(--fac-bar-2) 60%, var(--fac-bar-3));
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.16);
}

html[data-theme]:not(#_) .sidebar .sidebar-item {
  color: rgba(226, 232, 240, 0.72);
}

html[data-theme]:not(#_) .sidebar .sidebar-item:hover,
html[data-theme]:not(#_) .sidebar .sidebar-item.is-active {
  color: #ffffff;
}

/* ── Browser surfaces follow the theme ─────────────────────── */
html[data-theme]:not(#_) {
  color-scheme: light;
  scrollbar-color: var(--color-scrollbar-thumb) var(--color-scrollbar-track);
}

html[data-theme="dark-blue"]:not(#_),
html[data-theme="dark-gray"]:not(#_),
html[data-theme="solarized"]:not(#_) {
  color-scheme: dark;
}

html[data-theme]:not(#_) body {
  accent-color: var(--color-primary);
  caret-color: var(--color-primary);
}

html[data-theme]:not(#_) ::selection {
  background: rgba(var(--color-primary-rgb), 0.26);
  color: inherit;
}

/* ── Shared dark-shell UI (cards, tables, forms, menus) ────── */
html[data-theme] body.theme-dark .card {
  background: var(--color-surface);
  border-color: var(--color-border);
}

html[data-theme] body.theme-dark .table {
  color: var(--color-text);
}

html[data-theme] body.theme-dark .table tbody td {
  border-bottom-color: var(--color-border);
}

html[data-theme] body.theme-dark .form-control {
  background: var(--color-input-bg);
  border-color: var(--color-border);
  color: var(--color-text);
}

html[data-theme] body.theme-dark .form-control:focus {
  border-color: var(--color-primary);
  background: var(--color-input-bg);
}

html[data-theme] body.theme-dark :is(.user-menu, .lang-menu, .theme-menu) {
  background: var(--color-surface);
  border-color: var(--color-border);
}

html[data-theme] body.theme-dark :is(.user-menu-item, .lang-option, .theme-option) {
  color: var(--color-text);
}

/* ── Theme switcher (topbar) ──────────────────────────────── */
.topbar-theme {
  position: relative;
}

.theme-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(0, 0, 0, 0.12);
  color: inherit;
  cursor: pointer;
  font-size: var(--font-size-sm);
  transition: background 0.15s ease, border-color 0.15s ease;
}

.theme-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.55);
}

.topbar-theme.open .theme-btn {
  border-color: rgba(255, 255, 255, 0.65);
  background: rgba(255, 255, 255, 0.15);
}

.theme-menu {
  display: none;
  position: absolute;
  top: calc(100% + 0.35rem);
  right: 0;
  min-width: 220px;
  max-width: min(280px, calc(100vw - 1rem));
  background: var(--color-surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--color-border);
  padding: 0.35rem;
  z-index: 40;
}

.topbar-theme.open .theme-menu {
  display: block;
  animation: facFadeDown 0.15s ease;
}

@keyframes facFadeDown {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.theme-menu__group {
  margin: 0;
  padding: 0.45rem 0.6rem 0.2rem;
  color: var(--color-text-muted);
  font-size: 12px;
  font-weight: 700;
}

.theme-option {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  text-align: left;
  padding: 0.45rem 0.6rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  font-size: var(--font-size-sm);
  color: var(--color-text);
  font-family: inherit;
}

.theme-option:hover {
  background: color-mix(in srgb, var(--color-primary) 10%, transparent);
}

.theme-option.active {
  background: color-mix(in srgb, var(--color-primary) 16%, transparent);
  color: var(--color-primary-ink);
  font-weight: 600;
}

.theme-option.active::after {
  content: '\F26E';
  margin-left: auto;
  font-family: 'bootstrap-icons';
  font-size: 14px;
}

/* Swatch: header bar over page, with the action colour as a dot */
.theme-swatch {
  position: relative;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  flex-shrink: 0;
  border: 1px solid rgba(15, 23, 42, 0.16);
  overflow: hidden;
  background: linear-gradient(180deg, var(--sw-bar) 0 42%, var(--sw-page) 42% 100%);
}

.theme-swatch::after {
  content: '';
  position: absolute;
  right: 4px;
  bottom: 3px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--sw-act);
}

.swatch-light { --sw-bar: #1b3a5c; --sw-page: #f3f5f8; --sw-act: #2e75b6; }
.swatch-blue { --sw-bar: #11467d; --sw-page: #edf3fa; --sw-act: #1f6fc5; }
.swatch-calm { --sw-bar: #34495a; --sw-page: #eef1f3; --sw-act: #3a7a96; }
.swatch-dark-blue { --sw-bar: #0a111c; --sw-page: #142031; --sw-act: #3678bb; }
.swatch-dark-gray { --sw-bar: #0e1012; --sw-page: #1a1d21; --sw-act: #4479b0; }
.swatch-solarized { --sw-bar: #00212a; --sw-page: #073642; --sw-act: #1f75b0; }

html[data-theme="light"] .theme-btn,
html[data-theme="calm"] .theme-btn,
html[data-theme="blue"] .theme-btn {
  border-color: rgba(15, 23, 42, 0.2);
  background: rgba(255, 255, 255, 0.25);
  color: #0f172a;
}

html[data-theme="light"] .topbar .theme-btn,
html[data-theme="calm"] .topbar .theme-btn,
html[data-theme="blue"] .topbar .theme-btn {
  color: #f8fafc;
  border-color: rgba(255, 255, 255, 0.4);
  background: rgba(0, 0, 0, 0.15);
}

html[data-theme] body {
  --color-table-header-bg: var(--fac-table-head-bg);
}
